// GraphicView.cpp : implementation file
//

#include "stdafx.h"
#include "Sys001.h"
#include "GraphicView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CGraphicView

IMPLEMENT_DYNCREATE(CGraphicView, CView)

CGraphicView::CGraphicView()
{
    m_head = 0;
    m_tail = 0;
    m_pos.x = 0;
    m_pos.y = 0;
    m_size.cx = 0;
    m_size.cy = 0;
    a = 0;
}

CGraphicView::~CGraphicView()
{
}


BEGIN_MESSAGE_MAP(CGraphicView, CView)
    //{{AFX_MSG_MAP(CGraphicView)
    ON_WM_CREATE()
    ON_WM_TIMER()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGraphicView drawing

void CGraphicView::OnDraw(CDC* pDC)
{
#if 0
    CDocument* pDoc = GetDocument();
    // TODO: add draw code here
    pGraphic = (CWnd *)AfxGetMainWnd()->GetDlgItem(IDD_MYFORMVIEW_FORM)->GetDlgItem(IDC_GRAPHIC);


    CRect GraphicRect;
    pGraphic->GetClientRect(&GraphicRect);
    CSize GraphicSize;
    GraphicSize.cx = GraphicRect.right - GraphicRect.left;
    GraphicSize.cy = GraphicRect.bottom - GraphicRect.top;

    CDC MemDC;
    CBitmap MemBitmap;
    MemDC.CreateCompatibleDC(pDC);
    MemBitmap.CreateCompatibleBitmap(pDC, GraphicSize.cx, GraphicSize.cy);
    CBitmap *pOldBit = MemDC.SelectObject(&MemBitmap);

    MemDC.FillSolidRect(0, 0, GraphicSize.cx, GraphicSize.cy, RGB(0, 0, 0));

    CPen greenPen(PS_DASHDOT, 1, RGB(0, 0, 255));
    CPen *pOldPen = MemDC.SelectObject(&greenPen);

    MemDC.MoveTo(0, GraphicSize.cy/2);
    MemDC.LineTo(GraphicSize.cx, GraphicSize.cy/2);
    MemDC.MoveTo(GraphicSize.cx/2, 0);
    MemDC.LineTo(GraphicSize.cx/2, GraphicSize.cy);

    int x = 0;
    int y = 0;
    CPen redPen(PS_SOLID, 1, RGB(0, 255, 0));
    MemDC.SelectObject(&redPen);
    for(; x <= GraphicSize.cx && y <= GraphicSize.cy;)
    {
        MemDC.MoveTo(x + a, y);
        MemDC.LineTo( x +a + 1, y + 1); 
        x ++;
        y ++;
    }
    a = (a + 1)%50;

    CRect GraphicWindowRect;
    CRect ClientWindowRect;

    pGraphic->GetWindowRect(&GraphicWindowRect);
    GetWindowRect(&ClientWindowRect);

    CPoint GraphicPos;
    GraphicPos.x = GraphicWindowRect.left - ClientWindowRect.left;
    GraphicPos.y = GraphicWindowRect.top - ClientWindowRect.top;
    pDC->BitBlt(GraphicPos.x, GraphicPos.y, GraphicSize.cx, GraphicSize.cy, &MemDC, 0, 0, SRCCOPY);

    MemBitmap.DeleteObject();
    MemDC.DeleteDC();
#endif
}

void CGraphicView::SetPos(CPoint Point)
{
    m_pos = Point;
}
void CGraphicView::SetSize(CSize Size)
{
    m_size = Size;
}
CPoint CGraphicView::GetPos()
{
    return m_pos;
}
CSize CGraphicView::GetSize()
{
    return m_size;
}
void CGraphicView::AddData(unsigned int data)
{
    m_dataBuf[m_tail] = data;
    m_tail ++;
    m_tail %= 400;
    if(m_tail == m_head)
    {
        m_head ++;
    }
}
unsigned int CGraphicView::GetData()
{
    unsigned int ret_data = m_dataBuf[m_head];
    m_head++;
    m_head %= 400;
    return ret_data;
}
/////////////////////////////////////////////////////////////////////////////
// CGraphicView diagnostics

#ifdef _DEBUG
void CGraphicView::AssertValid() const
{
    CView::AssertValid();
}

void CGraphicView::Dump(CDumpContext& dc) const
{
    CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGraphicView message handlers

int CGraphicView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;
    
    // TODO: Add your specialized creation code here
    //SetTimer(0, 100, NULL);
    return 0;
}

void CGraphicView::OnTimer(UINT nIDEvent) 
{
    // TODO: Add your message handler code here and/or call default
    //CWnd *pGraphic = (CWnd *)AfxGetMainWnd()->GetDlgItem(IDD_MYFORMVIEW_FORM)->GetDlgItem(IDC_GRAPHIC);
    //pGraphic->InvalidateRect(NULL,true); 
    //pGraphic->UpdateWindow(); 
    
    CView::OnTimer(nIDEvent);
}
